﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Resources;
using System.Threading;
using System.Windows.Forms;
using PCSComProduct.Items.BO;
using PCSComProduction.WorkOrder.BO;
using PCSComUtils.Common;
using PCSComUtils.Common.BO;
using PCSComUtils.DataContext;
using PCSComUtils.PCSExc;
using PCSUtils.Log;
using PCSUtils.Utils;

namespace PCSProduction.WorkOrder
{
    /// <summary>
    /// Summary description for ConvertWorkorder.
    /// </summary>
    public class ConvertWorkorder : Form
    {
        // Fields
        private const string This = "PCSProduction.WorkOrder.ConvertWorkorder";
        private readonly ConvertWorkorderBO _boConvert = new ConvertWorkorderBO();
        private readonly Container components;
        private readonly Thread thrProcess;
        private Button btnClose;
        private Button btnHelp;
        private Button btnProcess;
        private Button btnProductCode;
        private CheckBox chkCTTC;
        private decimal decOrderQuantity;
        private DateTime dtmDueDate = DateTime.MinValue;
        private int intMainProductID;
        private int intSaleOrderDetailID;
        private int intSaleOrderMasterID;
        private Label lblProductCode;
        private PictureBox ptbImage;
        private string strProductCode = "";
        private TextBox txtProductCode;
        private ITM_Product voProduct = new ITM_Product();

        // Methods
        public ConvertWorkorder()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            try
            {
                if (FormControlComponents.CheckMandatory(txtProductCode))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Exclamation);
                    txtProductCode.Focus();
                }
                else
                {
                    var objProduct = new ProductItemInfoBO();
                    voProduct = objProduct.GetProductInfo(Convert.ToInt32(txtProductCode.Tag));
                    if (!voProduct.MakeItem)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_ITEM_IS_NONE_MAKE, MessageBoxIcon.Warning);
                        txtProductCode.Focus();
                        return;
                    }
                    // check F.Goods Code is converted yet
                    bool blnIsConverted = _boConvert.IsConverted(intSaleOrderDetailID);
                    if (blnIsConverted)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_SALE_IS_CONVERTED, MessageBoxIcon.Warning);
                        txtProductCode.Focus();
                        return;
                    }
                    ConvertToWorkOrder();
                }
            }
            catch (ThreadAbortException ex)
            {
                Logger.LogMessage(ex, "PCSProduction.WorkOrder.ConvertWorkorder.btnProcess_Click()", Level.DEBUG);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, "PCSProduction.WorkOrder.ConvertWorkorder.btnProcess_Click()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, "PCSProduction.WorkOrder.ConvertWorkorder.btnProcess_Click()", Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
        }

        private void btnProductCode_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnProductCode_Click()";
            try
            {
                DataRowView drwResult = null;
                Hashtable htbCondition = null;
                if ((sender is TextBox))
                {
                    drwResult = FormControlComponents.OpenSearchForm(SO_SaleOrderDetailTable.TABLE_NAME,
                                                                     SO_SaleOrderDetailTable.FGOODSCODE_FLD,
                                                                     txtProductCode.Text, htbCondition, false);
                }
                else
                {
                    drwResult = FormControlComponents.OpenSearchForm(SO_SaleOrderDetailTable.TABLE_NAME,
                                                                     SO_SaleOrderDetailTable.FGOODSCODE_FLD,
                                                                     txtProductCode.Text, htbCondition, true);
                }
                if (drwResult != null)
                {
                    txtProductCode.Text = drwResult[SO_SaleOrderDetailTable.FGOODSCODE_FLD].ToString();
                    txtProductCode.Tag = Convert.ToInt32(drwResult[SO_SaleOrderDetailTable.PRODUCTID_FLD]);
                    intSaleOrderMasterID = Convert.ToInt32(drwResult[SO_SaleOrderDetailTable.SALEORDERMASTERID_FLD]);
                    intSaleOrderDetailID = Convert.ToInt32(drwResult[SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD]);
                    dtmDueDate = Convert.ToDateTime(drwResult[SO_SaleOrderDetailTable.DUEDATE_FLD]);
                    try
                    {
                        decOrderQuantity = Convert.ToDecimal(drwResult[SO_SaleOrderDetailTable.WOQUANTITY_FLD]);
                        SwitchMode(false);
                    }
                    catch
                    {
                        SwitchMode(true);
                        throw new PCSException(ErrorCode.MESSAGE_WO_ORDERQUANTITY, METHOD_NAME, null);
                    }
                    btnProcess.Enabled = true;
                }
                else
                {
                    txtProductCode.Focus();
                    txtProductCode.SelectAll();
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException,
                                      "PCSProduction.WorkOrder.ConvertWorkorder.btnProductCode_Click()", Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, "PCSProduction.WorkOrder.ConvertWorkorder.btnProductCode_Click()", Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
        }

        private void CalculateProductionNorm_Closing(object sender, CancelEventArgs e)
        {
            try
            {
                if ((thrProcess != null) && (thrProcess.IsAlive || (thrProcess.ThreadState == ThreadState.Running)))
                {
                    var strMsg = new[] {Text};
                    DialogResult dlgResult = PCSMessageBox.Show(ErrorCode.MESSAGE_PROCESS_IS_RUNNING,
                                                                MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
                                                                MessageBoxDefaultButton.Button2, strMsg);
                    switch (dlgResult)
                    {
                        case DialogResult.OK:
                            try
                            {
                                thrProcess.Abort();
                            }
                            catch
                            {
                                e.Cancel = false;
                            }
                            break;
                        case DialogResult.Cancel:
                            e.Cancel = true;
                            break;
                    }
                }
                return;
            }
            catch (ThreadAbortException ex)
            {
                Logger.LogMessage(ex.Message,
                                  "PCSProduction.WorkOrder.ConvertWorkorder.CalculateProductionNorm_Closing()",
                                  Level.DEBUG);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException,
                                      "PCSProduction.WorkOrder.ConvertWorkorder.CalculateProductionNorm_Closing()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, "PCSProduction.WorkOrder.ConvertWorkorder.CalculateProductionNorm_Closing()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void CalculateProductionNorm_Load(object sender, EventArgs e)
        {
            try
            {
                var objSecurity = new Security();
                base.Name = "PCSProduction.WorkOrder.ConvertWorkorder";
                if (objSecurity.SetRightForUserOnForm(this, SystemProperty.UserName) == 0)
                {
                    base.Close();
                }
                else
                {
                    txtProductCode.Text = string.Empty;
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException,
                                      "PCSProduction.WorkOrder.ConvertWorkorder.CalculateProductionNorm_Load()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, "PCSProduction.WorkOrder.ConvertWorkorder.CalculateProductionNorm_Load()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
        }

        private void ConvertToWorkOrder()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                intMainProductID = Convert.ToInt32(txtProductCode.Tag);
                int intResult = ConvertToWorkOrder(intMainProductID);
                if (intResult == 1)
                {
                    var strMsg = new[] {Text};
                    PCSMessageBox.Show(ErrorCode.MESSAGE_TASK_COMPLETED, MessageBoxButtons.OK, MessageBoxIcon.Asterisk,
                                       MessageBoxDefaultButton.Button1, strMsg);
                }
                Cursor = Cursors.Default;
                SwitchMode(false);
            }
            catch (ThreadAbortException ex)
            {
                Logger.LogMessage(ex.Message, "PCSProduction.WorkOrder.ConvertWorkorder.Convert()", Level.DEBUG);
            }
            catch (PCSException ex)
            {
                if (ex.mCode == ErrorCode.MESSAGE_CANNOT_ROLL_UP)
                {
                    var strMsg = new[] {Text};
                    PCSMessageBox.Show(ErrorCode.MESSAGE_CANNOT_ROLL_UP, MessageBoxButtons.OK, MessageBoxIcon.Error,
                                       MessageBoxDefaultButton.Button1, strMsg);
                }
                else
                {
                    if (strProductCode != "")
                    {
                        MessageBox.Show("Bước sản xuất của ProductID:= " + strProductCode + " bị sai ");
                    }
                    else
                    {
                        PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                    }
                }
                try
                {
                    Logger.LogMessage(ex.CauseException, "PCSProduction.WorkOrder.ConvertWorkorder.Convert()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED);
                }
            }
            catch (Exception ex)
            {
                var strMsg = new[] {Text};
                PCSMessageBox.Show(ErrorCode.DUPLICATE_KEY, MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
                                   MessageBoxDefaultButton.Button1, strMsg);
                // PCSMessageBox.Show(ErrorCode.MESSAGE_CANNOT_ROLL_UP, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, strMsg);
                try
                {
                    Logger.LogMessage(ex, "PCSProduction.WorkOrder.ConvertWorkorder.Convert()", Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED);
                }
            }
            finally
            {
                Cursor = Cursors.Default;
                SwitchMode(false);
            }
        }

        private int ConvertToWorkOrder(int pintProductID)
        {
            DataSet dstWorkOrderDetail = _boConvert.GetWorkOrderDetail(0);
            DataTable dtbBOM = _boConvert.GetBOM(chkCTTC.Checked);
            var objProduct = new ProductItemInfoBO();
            voProduct = objProduct.GetProductInfo(pintProductID);
            DateTime dtmStartDate = new UtilsBO().GetDBDate();
            dstWorkOrderDetail = ConvertToWorkOrder(null, dstWorkOrderDetail, dtbBOM, decOrderQuantity, dtmStartDate);
            if (dstWorkOrderDetail != null)
            {
                var voMaster = new PRO_WorkOrderMaster {CCNID = SystemProperty.CCNID};
                string numberFormat = string.Format("{0}/YYYYMMDD####", txtProductCode.Text);
                string workOrderNo = FormControlComponents.GetNoByMask("PRO_WorkOrderMaster", "WorkOrderNo", string.Empty, numberFormat);
                voMaster.WorkOrderNo = workOrderNo;
                voMaster.MasterLocationID = SystemProperty.MasterLocationID;
                voMaster.TransDate = new UtilsBO().GetDBDate();
                voMaster.UserName = SystemProperty.UserName;
                var listDetail = new List<PRO_WorkOrderDetail>();
                int iLine = 0;
                foreach (DataRow dr in dstWorkOrderDetail.Tables[0].Rows)
                {
                    var workOrderDetail = new PRO_WorkOrderDetail();
                    if (dr[PRO_WorkOrderDetailTable.SALEORDERMASTERID_FLD] != null && dr[PRO_WorkOrderDetailTable.SALEORDERMASTERID_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.SaleOrderMasterID = Convert.ToInt32(dr[PRO_WorkOrderDetailTable.SALEORDERMASTERID_FLD]);
                    }
                    if (dr[PRO_WorkOrderDetailTable.SALEORDERDETAILID_FLD] != null && dr[PRO_WorkOrderDetailTable.SALEORDERDETAILID_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.SaleOrderDetailID = Convert.ToInt32(dr[PRO_WorkOrderDetailTable.SALEORDERDETAILID_FLD]);
                    }
                    if (dr[PRO_WorkOrderDetailTable.AGC_FLD] != null && dr[PRO_WorkOrderDetailTable.AGC_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.AGC = Convert.ToDecimal(dr[PRO_WorkOrderDetailTable.AGC_FLD]);
                    }
                    workOrderDetail.DueDate = Convert.ToDateTime(dr[PRO_WorkOrderDetailTable.DUEDATE_FLD]);
                    workOrderDetail.StartDate = Convert.ToDateTime(dr[PRO_WorkOrderDetailTable.STARTDATE_FLD]);
                    if (dr[PRO_WorkOrderDetailTable.ESTCST_FLD] != null && dr[PRO_WorkOrderDetailTable.ESTCST_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.EstCst = Convert.ToDecimal(dr[PRO_WorkOrderDetailTable.ESTCST_FLD]);
                    }
                    if (dr[PRO_WorkOrderDetailTable.FINCLOSEDATE_FLD] != null && dr[PRO_WorkOrderDetailTable.FINCLOSEDATE_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.FinCloseDate = Convert.ToDateTime(dr[PRO_WorkOrderDetailTable.FINCLOSEDATE_FLD]);
                    }
                    workOrderDetail.Group1 = dr[PRO_WorkOrderDetailTable.GROUP1_FLD] == null
                                                 ? string.Empty
                                                 : dr[PRO_WorkOrderDetailTable.GROUP1_FLD].ToString();
                    workOrderDetail.Group2 = dr[PRO_WorkOrderDetailTable.GROUP2_FLD] == null
                                                 ? string.Empty
                                                 : dr[PRO_WorkOrderDetailTable.GROUP2_FLD].ToString();
                    if (dr[PRO_WorkOrderDetailTable.GROUPQUANTITY_FLD] != null && dr[PRO_WorkOrderDetailTable.GROUPQUANTITY_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.GroupQuantity = Convert.ToDecimal(dr[PRO_WorkOrderDetailTable.GROUPQUANTITY_FLD]);
                    }
                    iLine++;
                    workOrderDetail.Line = iLine;
                    if (dr[PRO_WorkOrderDetailTable.MFGCLOSEDATE_FLD] != null && dr[PRO_WorkOrderDetailTable.MFGCLOSEDATE_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.MfgCloseDate = Convert.ToDateTime(dr[PRO_WorkOrderDetailTable.MFGCLOSEDATE_FLD]);
                    }
                    workOrderDetail.OrderQuantity = Convert.ToDecimal(dr[PRO_WorkOrderDetailTable.ORDERQUANTITY_FLD]);
                    if (dr[PRO_WorkOrderDetailTable.PRIORITY_FLD] != null && dr[PRO_WorkOrderDetailTable.PRIORITY_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.Priority = Convert.ToByte(dr[PRO_WorkOrderDetailTable.PRIORITY_FLD]);
                    }
                    workOrderDetail.ProductID = Convert.ToInt32(dr[PRO_WorkOrderDetailTable.PRODUCTID_FLD]);
                    if (dr[PRO_WorkOrderDetailTable.PRODUCTIONLINEID_FLD] != null && dr[PRO_WorkOrderDetailTable.PRODUCTIONLINEID_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.ProductionLineID = Convert.ToInt32(dr[PRO_WorkOrderDetailTable.PRODUCTIONLINEID_FLD]);
                    }
                    workOrderDetail.StockUMID = Convert.ToInt32(dr[PRO_WorkOrderDetailTable.STOCKUMID_FLD]);
                    if (dr[PRO_WorkOrderDetailTable.STATUS_FLD] != null && dr[PRO_WorkOrderDetailTable.STATUS_FLD].ToString() != string.Empty)
                    {
                        workOrderDetail.Status = Convert.ToByte(dr[PRO_WorkOrderDetailTable.STATUS_FLD]);
                    }
                    else
                    {
                        workOrderDetail.Status = 0;
                    }
                    listDetail.Add(workOrderDetail);
                }
                var workOrderBo = new WorkOrderBO();
                workOrderBo.InsertWorkOrder(voMaster, listDetail, strProductCode);
                return 1;
            }
            return 0;
        }

        private DataSet ConvertToWorkOrder(DataRow pdrowItem, DataSet pdstWorkOrderDetail, DataTable pdtbBOM, decimal pdecOrderQuantity, DateTime pdtmStartDate)
        {
            string strProductID = string.Empty;
            decimal decQuantity = 0M;
            if (pdrowItem == null)
            {
                strProductID = intMainProductID.ToString();
                DataRow drowNew = pdstWorkOrderDetail.Tables[0].NewRow();
                drowNew["ProductID"] = intMainProductID;
                drowNew["OrderQuantity"] = decOrderQuantity;
                drowNew["DueDate"] = dtmDueDate;
                drowNew["StartDate"] = pdtmStartDate;
                drowNew["StockUMID"] = voProduct.StockUMID;
                drowNew["SaleOrderMasterID"] = intSaleOrderMasterID;
                drowNew["SaleOrderDetailID"] = intSaleOrderDetailID;
                if (voProduct.ProductionLineID > 0)
                {
                    drowNew["ProductionLineID"] = voProduct.ProductionLineID;
                }
                else
                {
                    // display warning message and stop converting
                    PCSMessageBox.Show(ErrorCode.MESSAGE_CHECK_ITEM_SETUP, MessageBoxIcon.Warning);
                    return null;
                }
                drowNew["Group1"] = voProduct.Group1;
                drowNew["Group2"] = voProduct.Group2;
                drowNew["GroupQuantity"] = voProduct.GroupQuantity;
                drowNew["Status"] = WOLineStatus.Unreleased;
                pdstWorkOrderDetail.Tables[0].Rows.Add(drowNew);
                decQuantity = decOrderQuantity;
            }
            else if (Convert.ToBoolean(pdrowItem["MakeItem"]))
            {
                decimal decBOMQuantity = Convert.ToDecimal(pdrowItem["Quantity"]);
                strProductID = pdrowItem["ProductID"].ToString();
                string strFilter = "ProductID=" + strProductID + " AND StartDate='" + pdtmStartDate.ToString() +
                                   "' AND DueDate='" + dtmDueDate.ToString() + "'";
                decQuantity = pdecOrderQuantity*decBOMQuantity;
                if (pdstWorkOrderDetail.Tables[0].Select(strFilter).Length > 0)
                {
                    DataRow drowExist = pdstWorkOrderDetail.Tables[0].Select(strFilter)[0];
                    drowExist["OrderQuantity"] = Convert.ToDecimal(drowExist["OrderQuantity"]) + decQuantity;
                }
                else
                {
                    DataRow drowNew = pdstWorkOrderDetail.Tables[0].NewRow();
                    drowNew["ProductID"] = strProductID;
                    drowNew["OrderQuantity"] = decQuantity;
                    drowNew["DueDate"] = dtmDueDate;
                    drowNew["StartDate"] = pdtmStartDate;
                    drowNew["StockUMID"] = pdrowItem["StockUMID"];
                    drowNew["SaleOrderMasterID"] = intSaleOrderMasterID;
                    drowNew["SaleOrderDetailID"] = intSaleOrderDetailID;
                    // check production line. we will reaction later
                    if (Convert.ToInt32(pdrowItem["ProductionLineID"]) > 0)
                    {
                        drowNew["ProductionLineID"] = pdrowItem["ProductionLineID"];
                    }
                    else
                    {
                        return null;
                    }
                    drowNew[PRO_WorkOrderDetailTable.GROUP1_FLD] = pdrowItem[PRO_WorkOrderDetailTable.GROUP1_FLD];
                    drowNew[PRO_WorkOrderDetailTable.GROUP2_FLD] = pdrowItem[PRO_WorkOrderDetailTable.GROUP2_FLD];
                    drowNew[PRO_WorkOrderDetailTable.GROUPQUANTITY_FLD] = pdrowItem[PRO_WorkOrderDetailTable.GROUPQUANTITY_FLD];
                    drowNew["Status"] = WOLineStatus.Unreleased;
                    pdstWorkOrderDetail.Tables[0].Rows.Add(drowNew);
                }
            }
            if (strProductID != string.Empty)
            {
                foreach (DataRow drowItem in pdtbBOM.Select("ParentID= " + strProductID))
                {
                    ConvertToWorkOrder(drowItem, pdstWorkOrderDetail, pdtbBOM, decQuantity, pdtmStartDate);
                }
            }
            return pdstWorkOrderDetail;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            var resources = new ResourceManager(typeof (ConvertWorkorder));
            btnProductCode = new Button();
            txtProductCode = new TextBox();
            btnClose = new Button();
            btnHelp = new Button();
            lblProductCode = new Label();
            btnProcess = new Button();
            ptbImage = new PictureBox();
            chkCTTC = new CheckBox();
            SuspendLayout();
            // 
            // btnProductCode
            // 
            btnProductCode.AccessibleDescription = resources.GetString("btnProductCode.AccessibleDescription");
            btnProductCode.AccessibleName = resources.GetString("btnProductCode.AccessibleName");
            btnProductCode.Anchor = ((AnchorStyles) (resources.GetObject("btnProductCode.Anchor")));
            btnProductCode.BackgroundImage = ((Image) (resources.GetObject("btnProductCode.BackgroundImage")));
            btnProductCode.Dock = ((DockStyle) (resources.GetObject("btnProductCode.Dock")));
            btnProductCode.Enabled = ((bool) (resources.GetObject("btnProductCode.Enabled")));
            btnProductCode.FlatStyle = ((FlatStyle) (resources.GetObject("btnProductCode.FlatStyle")));
            btnProductCode.Font = ((Font) (resources.GetObject("btnProductCode.Font")));
            btnProductCode.Image = ((Image) (resources.GetObject("btnProductCode.Image")));
            btnProductCode.ImageAlign = ((ContentAlignment) (resources.GetObject("btnProductCode.ImageAlign")));
            btnProductCode.ImageIndex = ((int) (resources.GetObject("btnProductCode.ImageIndex")));
            btnProductCode.ImeMode = ((ImeMode) (resources.GetObject("btnProductCode.ImeMode")));
            btnProductCode.Location = ((Point) (resources.GetObject("btnProductCode.Location")));
            btnProductCode.Name = "btnProductCode";
            btnProductCode.RightToLeft = ((RightToLeft) (resources.GetObject("btnProductCode.RightToLeft")));
            btnProductCode.Size = ((Size) (resources.GetObject("btnProductCode.Size")));
            btnProductCode.TabIndex = ((int) (resources.GetObject("btnProductCode.TabIndex")));
            btnProductCode.Text = resources.GetString("btnProductCode.Text");
            btnProductCode.TextAlign = ((ContentAlignment) (resources.GetObject("btnProductCode.TextAlign")));
            btnProductCode.Visible = ((bool) (resources.GetObject("btnProductCode.Visible")));
            btnProductCode.Click += btnProductCode_Click;
            // 
            // txtProductCode
            // 
            txtProductCode.AccessibleDescription = resources.GetString("txtProductCode.AccessibleDescription");
            txtProductCode.AccessibleName = resources.GetString("txtProductCode.AccessibleName");
            txtProductCode.Anchor = ((AnchorStyles) (resources.GetObject("txtProductCode.Anchor")));
            txtProductCode.AutoSize = ((bool) (resources.GetObject("txtProductCode.AutoSize")));
            txtProductCode.BackgroundImage = ((Image) (resources.GetObject("txtProductCode.BackgroundImage")));
            txtProductCode.Dock = ((DockStyle) (resources.GetObject("txtProductCode.Dock")));
            txtProductCode.Enabled = ((bool) (resources.GetObject("txtProductCode.Enabled")));
            txtProductCode.Font = ((Font) (resources.GetObject("txtProductCode.Font")));
            txtProductCode.ImeMode = ((ImeMode) (resources.GetObject("txtProductCode.ImeMode")));
            txtProductCode.Location = ((Point) (resources.GetObject("txtProductCode.Location")));
            txtProductCode.MaxLength = ((int) (resources.GetObject("txtProductCode.MaxLength")));
            txtProductCode.Multiline = ((bool) (resources.GetObject("txtProductCode.Multiline")));
            txtProductCode.Name = "txtProductCode";
            txtProductCode.PasswordChar = ((char) (resources.GetObject("txtProductCode.PasswordChar")));
            txtProductCode.RightToLeft = ((RightToLeft) (resources.GetObject("txtProductCode.RightToLeft")));
            txtProductCode.ScrollBars = ((ScrollBars) (resources.GetObject("txtProductCode.ScrollBars")));
            txtProductCode.Size = ((Size) (resources.GetObject("txtProductCode.Size")));
            txtProductCode.TabIndex = ((int) (resources.GetObject("txtProductCode.TabIndex")));
            txtProductCode.Text = resources.GetString("txtProductCode.Text");
            txtProductCode.TextAlign = ((HorizontalAlignment) (resources.GetObject("txtProductCode.TextAlign")));
            txtProductCode.Visible = ((bool) (resources.GetObject("txtProductCode.Visible")));
            txtProductCode.WordWrap = ((bool) (resources.GetObject("txtProductCode.WordWrap")));
            txtProductCode.KeyDown += txtProductCode_KeyDown;
            txtProductCode.Validating += txtProductCode_Validating;
            // 
            // btnClose
            // 
            btnClose.AccessibleDescription = resources.GetString("btnClose.AccessibleDescription");
            btnClose.AccessibleName = resources.GetString("btnClose.AccessibleName");
            btnClose.Anchor = ((AnchorStyles) (resources.GetObject("btnClose.Anchor")));
            btnClose.BackgroundImage = ((Image) (resources.GetObject("btnClose.BackgroundImage")));
            //this.btnClose.DialogResult = DialogResult.Cancel;
            btnClose.Dock = ((DockStyle) (resources.GetObject("btnClose.Dock")));
            btnClose.Enabled = ((bool) (resources.GetObject("btnClose.Enabled")));
            btnClose.FlatStyle = ((FlatStyle) (resources.GetObject("btnClose.FlatStyle")));
            btnClose.Font = ((Font) (resources.GetObject("btnClose.Font")));
            btnClose.Image = ((Image) (resources.GetObject("btnClose.Image")));
            btnClose.ImageAlign = ((ContentAlignment) (resources.GetObject("btnClose.ImageAlign")));
            btnClose.ImageIndex = ((int) (resources.GetObject("btnClose.ImageIndex")));
            btnClose.ImeMode = ((ImeMode) (resources.GetObject("btnClose.ImeMode")));
            btnClose.Location = ((Point) (resources.GetObject("btnClose.Location")));
            btnClose.Name = "btnClose";
            btnClose.RightToLeft = ((RightToLeft) (resources.GetObject("btnClose.RightToLeft")));
            btnClose.Size = ((Size) (resources.GetObject("btnClose.Size")));
            btnClose.TabIndex = ((int) (resources.GetObject("btnClose.TabIndex")));
            btnClose.Text = resources.GetString("btnClose.Text");
            btnClose.TextAlign = ((ContentAlignment) (resources.GetObject("btnClose.TextAlign")));
            btnClose.Visible = ((bool) (resources.GetObject("btnClose.Visible")));
            btnClose.Click += btnClose_Click;
            // 
            // btnHelp
            // 
            btnHelp.AccessibleDescription = resources.GetString("btnHelp.AccessibleDescription");
            btnHelp.AccessibleName = resources.GetString("btnHelp.AccessibleName");
            btnHelp.Anchor = ((AnchorStyles) (resources.GetObject("btnHelp.Anchor")));
            btnHelp.BackgroundImage = ((Image) (resources.GetObject("btnHelp.BackgroundImage")));
            btnHelp.Dock = ((DockStyle) (resources.GetObject("btnHelp.Dock")));
            btnHelp.Enabled = ((bool) (resources.GetObject("btnHelp.Enabled")));
            btnHelp.FlatStyle = ((FlatStyle) (resources.GetObject("btnHelp.FlatStyle")));
            btnHelp.Font = ((Font) (resources.GetObject("btnHelp.Font")));
            btnHelp.Image = ((Image) (resources.GetObject("btnHelp.Image")));
            btnHelp.ImageAlign = ((ContentAlignment) (resources.GetObject("btnHelp.ImageAlign")));
            btnHelp.ImageIndex = ((int) (resources.GetObject("btnHelp.ImageIndex")));
            btnHelp.ImeMode = ((ImeMode) (resources.GetObject("btnHelp.ImeMode")));
            btnHelp.Location = ((Point) (resources.GetObject("btnHelp.Location")));
            btnHelp.Name = "btnHelp";
            btnHelp.RightToLeft = ((RightToLeft) (resources.GetObject("btnHelp.RightToLeft")));
            btnHelp.Size = ((Size) (resources.GetObject("btnHelp.Size")));
            btnHelp.TabIndex = ((int) (resources.GetObject("btnHelp.TabIndex")));
            btnHelp.Text = resources.GetString("btnHelp.Text");
            btnHelp.TextAlign = ((ContentAlignment) (resources.GetObject("btnHelp.TextAlign")));
            btnHelp.Visible = ((bool) (resources.GetObject("btnHelp.Visible")));
            // 
            // lblProductCode
            // 
            lblProductCode.AccessibleDescription = resources.GetString("lblProductCode.AccessibleDescription");
            lblProductCode.AccessibleName = resources.GetString("lblProductCode.AccessibleName");
            lblProductCode.Anchor = ((AnchorStyles) (resources.GetObject("lblProductCode.Anchor")));
            lblProductCode.AutoSize = ((bool) (resources.GetObject("lblProductCode.AutoSize")));
            lblProductCode.Dock = ((DockStyle) (resources.GetObject("lblProductCode.Dock")));
            lblProductCode.Enabled = ((bool) (resources.GetObject("lblProductCode.Enabled")));
            lblProductCode.Font = ((Font) (resources.GetObject("lblProductCode.Font")));
            lblProductCode.ForeColor = Color.Maroon;
            lblProductCode.Image = ((Image) (resources.GetObject("lblProductCode.Image")));
            lblProductCode.ImageAlign = ((ContentAlignment) (resources.GetObject("lblProductCode.ImageAlign")));
            lblProductCode.ImageIndex = ((int) (resources.GetObject("lblProductCode.ImageIndex")));
            lblProductCode.ImeMode = ((ImeMode) (resources.GetObject("lblProductCode.ImeMode")));
            lblProductCode.Location = ((Point) (resources.GetObject("lblProductCode.Location")));
            lblProductCode.Name = "lblProductCode";
            lblProductCode.RightToLeft = ((RightToLeft) (resources.GetObject("lblProductCode.RightToLeft")));
            lblProductCode.Size = ((Size) (resources.GetObject("lblProductCode.Size")));
            lblProductCode.TabIndex = ((int) (resources.GetObject("lblProductCode.TabIndex")));
            lblProductCode.Text = resources.GetString("lblProductCode.Text");
            lblProductCode.TextAlign = ((ContentAlignment) (resources.GetObject("lblProductCode.TextAlign")));
            lblProductCode.Visible = ((bool) (resources.GetObject("lblProductCode.Visible")));
            // 
            // btnProcess
            // 
            btnProcess.AccessibleDescription = resources.GetString("btnProcess.AccessibleDescription");
            btnProcess.AccessibleName = resources.GetString("btnProcess.AccessibleName");
            btnProcess.Anchor = ((AnchorStyles) (resources.GetObject("btnProcess.Anchor")));
            btnProcess.BackgroundImage = ((Image) (resources.GetObject("btnProcess.BackgroundImage")));
            btnProcess.Dock = ((DockStyle) (resources.GetObject("btnProcess.Dock")));
            btnProcess.Enabled = ((bool) (resources.GetObject("btnProcess.Enabled")));
            btnProcess.FlatStyle = ((FlatStyle) (resources.GetObject("btnProcess.FlatStyle")));
            btnProcess.Font = ((Font) (resources.GetObject("btnProcess.Font")));
            btnProcess.Image = ((Image) (resources.GetObject("btnProcess.Image")));
            btnProcess.ImageAlign = ((ContentAlignment) (resources.GetObject("btnProcess.ImageAlign")));
            btnProcess.ImageIndex = ((int) (resources.GetObject("btnProcess.ImageIndex")));
            btnProcess.ImeMode = ((ImeMode) (resources.GetObject("btnProcess.ImeMode")));
            btnProcess.Location = ((Point) (resources.GetObject("btnProcess.Location")));
            btnProcess.Name = "btnProcess";
            btnProcess.RightToLeft = ((RightToLeft) (resources.GetObject("btnProcess.RightToLeft")));
            btnProcess.Size = ((Size) (resources.GetObject("btnProcess.Size")));
            btnProcess.TabIndex = ((int) (resources.GetObject("btnProcess.TabIndex")));
            btnProcess.Text = resources.GetString("btnProcess.Text");
            btnProcess.TextAlign = ((ContentAlignment) (resources.GetObject("btnProcess.TextAlign")));
            btnProcess.Visible = ((bool) (resources.GetObject("btnProcess.Visible")));
            btnProcess.Click += btnProcess_Click;
            // 
            // ptbImage
            // 
            ptbImage.AccessibleDescription = resources.GetString("ptbImage.AccessibleDescription");
            ptbImage.AccessibleName = resources.GetString("ptbImage.AccessibleName");
            ptbImage.Anchor = ((AnchorStyles) (resources.GetObject("ptbImage.Anchor")));
            ptbImage.BackgroundImage = ((Image) (resources.GetObject("ptbImage.BackgroundImage")));
            ptbImage.Dock = ((DockStyle) (resources.GetObject("ptbImage.Dock")));
            ptbImage.Enabled = ((bool) (resources.GetObject("ptbImage.Enabled")));
            ptbImage.Font = ((Font) (resources.GetObject("ptbImage.Font")));
            ptbImage.Image = ((Image) (resources.GetObject("ptbImage.Image")));
            ptbImage.ImeMode = ((ImeMode) (resources.GetObject("ptbImage.ImeMode")));
            ptbImage.Location = ((Point) (resources.GetObject("ptbImage.Location")));
            ptbImage.Name = "ptbImage";
            ptbImage.RightToLeft = ((RightToLeft) (resources.GetObject("ptbImage.RightToLeft")));
            ptbImage.Size = ((Size) (resources.GetObject("ptbImage.Size")));
            ptbImage.SizeMode = ((PictureBoxSizeMode) (resources.GetObject("ptbImage.SizeMode")));
            ptbImage.TabIndex = ((int) (resources.GetObject("ptbImage.TabIndex")));
            ptbImage.TabStop = false;
            ptbImage.Text = resources.GetString("ptbImage.Text");
            ptbImage.Visible = ((bool) (resources.GetObject("ptbImage.Visible")));
            // 
            // chkCTTC
            // 
            chkCTTC.AccessibleDescription = resources.GetString("chkCTTC.AccessibleDescription");
            chkCTTC.AccessibleName = resources.GetString("chkCTTC.AccessibleName");
            chkCTTC.Anchor = ((AnchorStyles) (resources.GetObject("chkCTTC.Anchor")));
            chkCTTC.Appearance = ((Appearance) (resources.GetObject("chkCTTC.Appearance")));
            chkCTTC.BackgroundImage = ((Image) (resources.GetObject("chkCTTC.BackgroundImage")));
            chkCTTC.CheckAlign = ((ContentAlignment) (resources.GetObject("chkCTTC.CheckAlign")));
            chkCTTC.Dock = ((DockStyle) (resources.GetObject("chkCTTC.Dock")));
            chkCTTC.Enabled = ((bool) (resources.GetObject("chkCTTC.Enabled")));
            chkCTTC.FlatStyle = ((FlatStyle) (resources.GetObject("chkCTTC.FlatStyle")));
            chkCTTC.Font = ((Font) (resources.GetObject("chkCTTC.Font")));
            chkCTTC.Image = ((Image) (resources.GetObject("chkCTTC.Image")));
            chkCTTC.ImageAlign = ((ContentAlignment) (resources.GetObject("chkCTTC.ImageAlign")));
            chkCTTC.ImageIndex = ((int) (resources.GetObject("chkCTTC.ImageIndex")));
            chkCTTC.ImeMode = ((ImeMode) (resources.GetObject("chkCTTC.ImeMode")));
            chkCTTC.Location = ((Point) (resources.GetObject("chkCTTC.Location")));
            chkCTTC.Name = "chkCTTC";
            chkCTTC.RightToLeft = ((RightToLeft) (resources.GetObject("chkCTTC.RightToLeft")));
            chkCTTC.Size = ((Size) (resources.GetObject("chkCTTC.Size")));
            chkCTTC.TabIndex = ((int) (resources.GetObject("chkCTTC.TabIndex")));
            chkCTTC.Text = resources.GetString("chkCTTC.Text");
            chkCTTC.TextAlign = ((ContentAlignment) (resources.GetObject("chkCTTC.TextAlign")));
            chkCTTC.Visible = ((bool) (resources.GetObject("chkCTTC.Visible")));
            // 
            // ConvertWorkorder
            // 
            AccessibleDescription = resources.GetString("$this.AccessibleDescription");
            AccessibleName = resources.GetString("$this.AccessibleName");
            AutoScaleBaseSize = ((Size) (resources.GetObject("$this.AutoScaleBaseSize")));
            AutoScroll = ((bool) (resources.GetObject("$this.AutoScroll")));
            AutoScrollMargin = ((Size) (resources.GetObject("$this.AutoScrollMargin")));
            AutoScrollMinSize = ((Size) (resources.GetObject("$this.AutoScrollMinSize")));
            BackgroundImage = ((Image) (resources.GetObject("$this.BackgroundImage")));
            CancelButton = btnClose;
            ClientSize = ((Size) (resources.GetObject("$this.ClientSize")));
            Controls.Add(chkCTTC);
            Controls.Add(ptbImage);
            Controls.Add(btnProductCode);
            Controls.Add(txtProductCode);
            Controls.Add(btnClose);
            Controls.Add(btnHelp);
            Controls.Add(lblProductCode);
            Controls.Add(btnProcess);
            Enabled = ((bool) (resources.GetObject("$this.Enabled")));
            Font = ((Font) (resources.GetObject("$this.Font")));
            Icon = ((Icon) (resources.GetObject("$this.Icon")));
            ImeMode = ((ImeMode) (resources.GetObject("$this.ImeMode")));
            Location = ((Point) (resources.GetObject("$this.Location")));
            MaximizeBox = false;
            MaximumSize = ((Size) (resources.GetObject("$this.MaximumSize")));
            MinimumSize = ((Size) (resources.GetObject("$this.MinimumSize")));
            Name = "ConvertWorkorder";
            RightToLeft = ((RightToLeft) (resources.GetObject("$this.RightToLeft")));
            StartPosition = ((FormStartPosition) (resources.GetObject("$this.StartPosition")));
            Text = resources.GetString("$this.Text");
            Closing += CalculateProductionNorm_Closing;
            Load += CalculateProductionNorm_Load;
            ResumeLayout(false);
        }

        private void SwitchMode(bool pblnProcessing)
        {
            txtProductCode.Enabled = !pblnProcessing;
            btnProcess.Enabled = !pblnProcessing;
            ptbImage.Visible = pblnProcessing;
        }

        private void txtProductCode_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F4)
                {
                    btnProductCode_Click(null, null);
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException,
                                      "PCSProduction.WorkOrder.ConvertWorkorder.txtProductCode_KeyDown()", Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, "PCSProduction.WorkOrder.ConvertWorkorder.txtProductCode_KeyDown()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
        }

        private void txtProductCode_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (txtProductCode.Modified)
                {
                    if (txtProductCode.Text != string.Empty)
                    {
                        btnProductCode_Click(sender, e);
                    }
                    else
                    {
                        txtProductCode.Tag = null;
                        btnProcess.Enabled = false;
                    }
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException,
                                      "PCSProduction.WorkOrder.ConvertWorkorder.txtProductCode_Validating()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, "PCSProduction.WorkOrder.ConvertWorkorder.txtProductCode_Validating()",
                                      Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_NOT_SUPPORTED, MessageBoxIcon.Error);
                }
            }
        }
    }
}